//
//  MallHomeMoreChannelView.m
//  Yanwei
//
//  Created by weijunjie on 2018/11/12.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "MallHomeMoreChannelView.h"
#import "UIView+XWAddForRoundedCorner.h"
#import "MallHomeChannelListModel.h"

@interface MallHomeMoreChannelView()
@property (nonatomic, strong) UIView *toolView;
@property (nonatomic, strong) UIButton *seletedBtn;
@end

@implementation MallHomeMoreChannelView

- (void)showWithItemArray:(NSArray *)itemArray index:(NSInteger)index {
    NSInteger count = itemArray.count;
    
    NSUInteger column = 4; //  列数
    if (ScreenWidth == 320) { // 小屏手机3列
        column = 3;
    }
    CGFloat topMargin = 10; // 顶部间距
    CGFloat bottomMargin = 20; // 底部间距
    CGFloat width = 80; // 宽度
    CGFloat height = 35; // 高度
    CGFloat lrMargin = 15; // 左右间距
    CGFloat lrPadding = (ScreenWidth - column * width - 2 * lrMargin) / (column - 1); // 横向间距
    NSUInteger row = 0; // 当前行
    NSUInteger col = 0; // 当前列
    
    NSString *title = @"";
    for (int i = 0; i < count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i;
        MallHomeChannelListModel *listModel = itemArray[i];
        title = listModel.name;
        if (title.length > 4) {
            title = [title substringToIndex:4];
        }
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:Color34 forState:UIControlStateNormal];

        btn.titleLabel.font = APPFONT(16);
        [btn xw_roundedCornerWithCornerRadii:CGSizeMake(2, 2) cornerColor:[UIColor whiteColor] corners:UIRectCornerAllCorners borderColor:Color244 borderWidth:0.5];
        [self.toolView addSubview:btn];
        if (index == i) {
            [btn setTitleColor:Color175030 forState:UIControlStateNormal];
            [btn xw_roundedCornerWithCornerRadii:CGSizeMake(2, 2) cornerColor:[UIColor whiteColor] corners:UIRectCornerAllCorners borderColor:Color175030 borderWidth:0.5];
            self.seletedBtn = btn;
        }
        row = i / column;
        col = i % column;
        btn.frame = CGRectMake(col * (width + lrPadding) + lrMargin, row * (height + topMargin) + topMargin, width, height);
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == count - 1) {
            _toolView.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(btn.frame) + bottomMargin);
            self.toolView.transform = CGAffineTransformMakeTranslation(0, -self.toolView.height);
            [UIView animateWithDuration:0.25 animations:^{
                self.toolView.transform = CGAffineTransformIdentity;
            }];
        }
    }
}

#pragma mark - event
- (void)btnAction:(UIButton *)btn {
    self.userInteractionEnabled = NO;
    [self.seletedBtn setTitleColor:Color34 forState:UIControlStateNormal];
    [btn xw_roundedCornerWithCornerRadii:CGSizeMake(2, 2) cornerColor:[UIColor whiteColor] corners:UIRectCornerAllCorners borderColor:Color244 borderWidth:0.5];
    self.seletedBtn = btn;
    [self.seletedBtn setTitleColor:Color175030 forState:UIControlStateNormal];
    [btn xw_roundedCornerWithCornerRadii:CGSizeMake(2, 2) cornerColor:[UIColor whiteColor] corners:UIRectCornerAllCorners borderColor:Color175030 borderWidth:0.5];
    if (self.moreChannelViewBlock) {
        self.moreChannelViewBlock(YES, btn.tag);
    }
}

#pragma mark - 懒加载
- (UIView *)toolView {
    if (!_toolView) {
        _toolView = [[UIView alloc] init];
        _toolView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_toolView];
        self.layer.masksToBounds = YES;
    }
    return _toolView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([touches anyObject].view == self.toolView) return;
    if (self.moreChannelViewBlock) {
        self.moreChannelViewBlock(NO,0);
    }
    self.userInteractionEnabled = NO;
}

- (void)disMissView {
    [UIView animateWithDuration:0.25 animations:^{
        self.toolView.transform = CGAffineTransformMakeTranslation(0, -300);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
